var data = require('../../data/post-data.js')
var app = getApp()

Page({
    data:{
        isPlayMusic:false
    },
    //页面加载时的操作
    onLoad:function(options){

        var postCollect

        var postId = options.postId

            this.setData({

                post:data.posts[postId],
                postId:postId 
            })

            this.onBackgroundMusic()

            var postsCollected = wx.getStorageSync('postsCollected')

            var postView = wx.getStorageSync('postsView')

                if(postView){
                    
                    postView[postId] = isNaN(postView[postId])?1:postView[postId]+1

                }else{

                    postView = {}
                    postView[postId] = 1
                   
                }

                 wx.setStorageSync('postsView',postView)

                 
            //获取本地存储的相关操作,与收藏相关的的操作
            if(postsCollected){

                postCollect = postsCollected[postId]

            }else{

                postsCollected = {}

                postCollect = postsCollected[postId]= false

                wx.setStorageSync('postsCollected',postsCollected)
            }
                this.setData({

                    postCollect:postCollect
                    
                })
            
            if(app.globalData.g_isPlayingMusic && app.globalData.g_isPlayingMusicId === postId){

                this.setData({
                    isPlayMusic:true
                })
            }
          
    },
    //监听音乐播放和停止的方法
    onBackgroundMusic:function(){

        //把this指定给一个变量，方便后面调用
        var that = this

          //监听到音乐播放
        wx.onBackgroundAudioPlay(function() {
        
            that.setData({
                isPlayMusic:true
            })

            app.globalData.g_isPlayingMusic = true
            app.globalData.g_isPlayingMusicId = that.data.postId
        })
        //监听到音乐停止
        wx.onBackgroundAudioPause(function() {
            
            that.setData({
                isPlayMusic:false
            })

            app.globalData.g_isPlayingMusic = false
        })

        wx.onBackgroundAudioStop(function() {
            
             that.setData({
                isPlayMusic:false
            })

            app.globalData.g_isPlayingMusic = false
        })
    },
    //点击收藏按钮的操作
    onCollectionTap:function(event){

       var postsCollected =  wx.getStorageSync('postsCollected')
       var postId = this.data.postId
        postsCollected[postId] = !postsCollected[postId]
        wx.setStorageSync('postsCollected',postsCollected)

        //重新渲染页面上的的数据，使按钮样式发生变化
        this.setData({
            postCollect:postsCollected[postId]
        })

        this.collectionByShowToast(postsCollected[postId])

    },
    //分享的相关操作
    onShareTap:function(event){
          var itemLists = ['QQ空间','朋友圈','腾讯微博','新浪微博']
        wx.showActionSheet({
            itemList:itemLists,
            success:function(res){
                console.log(res.tapIndex)
                if(!isNaN(res.tapIndex)){
                    console.log(res.tapIndex)
                    wx.showModal({
                        title:'收藏',
                        content:'要把本片文章分享到'+itemLists[res.tapIndex]+'吗？'
                    })
                }
            }
        })
    },
    //控制收藏蒙版的显隐
    collectionByShowToast:function(postCollect){
        
         wx.showToast({
            title:postCollect?'收藏成功':'取消成功',
            duration:1000
        })
    },
    //点击音乐播放的相关操作
    onPlayMusic:function(){
        var musicFlag = this.data.isPlayMusic
        var post = this.data.post
        var that = this
        if(!musicFlag){

                  wx.playBackgroundAudio({ 
dataUrl:post.music.url,
          title:post.music.title,
          coverImg:post.music.coverImg,
          success: function(res){
              musicFlag = true
              that.setData({
                  isPlayMusic:true
              })
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
        }else{
            wx.pauseBackgroundAudio({
              success: function(res){
                  musicFlag = false
                  that.setData({
                      isPlayMusic:false
                  })
              },
              fail: function() {
                
              },
              complete: function() {
                // complete
              }
            })
        }
    }
})